﻿using Common.Entitys.CommonLogs;
using Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ProtoBuf;
using Newtonsoft.Json;
using Common.Entitys.Messages;

namespace Common.Utlis
{
    public class SerializeHelp
    {
        //public static byte[] SerializeToByteArray(object obj)
        //{
        //    try
        //    {
        //        byte[] buff;
        //        using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
        //        {
        //            System.Runtime.Serialization.IFormatter iFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        //            iFormatter.Serialize(ms, obj);
        //            buff = ms.GetBuffer();

        //        }
        //        return buff;
        //    }
        //    catch (Exception e)
        //    {
        //        ErrorLogEntity errorLogEntity = new ErrorLogEntity("序列化化异常", e);
        //        LogHelp.AddLog<ErrorLogEntity>(errorLogEntity);
        //        return null;
        //    }

        //}

        //public static object ByteToObj(byte[] buff)
        //{
        //    object obj;
        //    using (System.IO.MemoryStream ms = new System.IO.MemoryStream(buff))
        //    {
        //        System.Runtime.Serialization.IFormatter iFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        //        obj = iFormatter.Deserialize(ms);

        //    }
        //    return obj;
        //}

        public static byte[] SerializeToByteArray1(object obj)
        {
            try
            {
                byte[] buff;
              BinaryFormatter.BinaryConverter conv = new BinaryFormatter.BinaryConverter();
              buff= conv.Serialize(obj);
                return buff;
            }
            catch (Exception e)
            {
                ErrorLogEntity errorLogEntity = new ErrorLogEntity("序列化化异常", e);
                LogHelp.AddLog<ErrorLogEntity>(errorLogEntity);
                return null;
            }

        }

        public static object ByteToObj1(byte[] buff)
        {
            object obj;
            BinaryFormatter.BinaryConverter conv = new BinaryFormatter.BinaryConverter();
            obj= conv.Deserialize<IPCMsg>(buff);
            return obj;
        }

        public static byte[] SerializeToByteArray2(object obj)
        {
            try
            {
                byte[] buff;
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                ProtoBuf.Serializer.Serialize<IPCMsg>(ms, (IPCMsg)obj);
                buff= ms.GetBuffer();
                ms.Dispose();
                return buff;
            }
            catch (Exception e)
            {
                ErrorLogEntity errorLogEntity = new ErrorLogEntity("序列化化异常", e);
                LogHelp.AddLog<ErrorLogEntity>(errorLogEntity);
                return null;
            }

        }

        public static object ByteToObj2(byte[] buff)
        {
            object obj;
            ReadOnlySpan<byte> buff2= buff;
            obj= ProtoBuf.Serializer.Deserialize<IPCMsg>(buff2);
            return obj;
        }

        public static byte[] SerializeToByteArray(object obj)
        {
            try
            {
                
                string json=JsonConvert.SerializeObject(obj);

                var cc = JsonConvert.DeserializeObject<IPCMsg>(json);
                byte[] buff = Encoding.UTF8.GetBytes(json);
                IPCMsg msg= obj as IPCMsg;
               string s=  msg.Obj.GetType().Name;
                Type type2 = msg.Obj.GetType();
                Type type = Type.GetType(msg.AssemblyQualifiedName);
                
                Type type1 = Type.GetType(s);

                return buff;
            }
            catch (Exception e)
            {
                ErrorLogEntity errorLogEntity = new ErrorLogEntity("序列化化异常", e);
                LogHelp.AddLog<ErrorLogEntity>(errorLogEntity);
                return null;
            }

        }

        public static object ByteToObj(byte[] buff)
        {
            try
            {
                string json = Encoding.UTF8.GetString(buff);
                IPCMsg iPCMsg = JsonConvert.DeserializeObject<IPCMsg>(json);
                Type type = Type.GetType(iPCMsg.AssemblyQualifiedName);
                iPCMsg.Obj= JsonConvert.DeserializeObject(iPCMsg.Obj.ToString(), type);
                return iPCMsg;
            }
            catch (Exception e)
            {
                ErrorLogEntity errorLogEntity = new ErrorLogEntity("反序列化化异常", e);
                LogHelp.AddLog<ErrorLogEntity>(errorLogEntity);
                return null;
            }
           
        }
    }
}
